package com.pimwi.view.preloader
{
    import flash.display.BitmapData;
    import flash.display.Graphics;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Matrix;
    import flash.text.TextField;
    import flash.utils.ByteArray;
    
    import mx.graphics.codec.PNGEncoder;

    public class CustomProgressBar extends Loader
    {
        private var m_Ready: Boolean;
        private var m_Progress: Number;
        private var m_BitmapData: BitmapData;

        // Constant to change to fit your own layout
        private static var progressWidth: int = 200; // Progress bar width
        private static var leftMargin: int = 1;      // Left Margin
        private static var rightMargin: int = 2;     // Right Margin
        private static var topMargin: int = 1;       // Top Margin
        private static var bottomMargin: int = 1;    // Bottom Margin
        private static var progressHeight: int = 18; // Progress bar height

        private static var progressBarBackground: uint = 0xFFFFFF;
        private static var progressBarOuterBorder: uint = 0x323232;
        private static var progressBarColor: uint = 0xffBB33;
        private static var ProgressBarInnerColor: uint = 0xFFFFFF;

        public function CustomProgressBar(): void
        {
          super();
          m_Progress = 0;
          this.addEventListener(Event.RENDER, renderEventHandler);
        }

        private function renderEventHandler(event: Event): void
        {
        }

        public function refreshProgressBar(): void
        {
          m_BitmapData = drawProgress(); // Create the bitmapdata object
          var encoder: PNGEncoder = new PNGEncoder();
          var byteArray: ByteArray = encoder.encode(m_BitmapData); // Encode the bitmapdata to a bytearray
          this.loadBytes(byteArray); // Draw the bitmap on the loader object
        }

        public function getWidth(): Number
        {
          return leftMargin + progressWidth + rightMargin;
        }

        public function getHeight(): Number
        {
          return topMargin + 22 + bottomMargin;
        }

        private function drawProgress(): BitmapData
        {
          // Create the bitmap class object
          var bitmapData: BitmapData = new BitmapData(getWidth(), getHeight(), true, 0);

          // Draw the Progress Bar
          var sprite: Sprite = new Sprite();
          var graph: Graphics = sprite.graphics;

          // Draw the progress bar background
          graph.beginFill(progressBarBackground);
          graph.lineStyle(1, progressBarOuterBorder, 1, true);
          var containerWidth: Number = progressWidth;
          var px: int = getWidth() - rightMargin - progressWidth;
          var py: int = (getHeight() - progressHeight) / 2;
          graph.drawRoundRect(px, py, containerWidth, progressHeight, 0);
          containerWidth -= 4;
          var progressWidth: Number = containerWidth * m_Progress / 100;
          graph.beginFill(progressBarColor);
          graph.lineStyle(1, ProgressBarInnerColor, 1, true);
          //graph.beginGradientFill(GradientType.LINEAR,[0xFF6600,0x000000], [1.0,1.0], [0xFF,0xFF]);  
          graph.drawRoundRect(px + 2, py + 2, progressWidth, progressHeight - 4, 0);

          //Construct the Text Field Object and put the progress value in it
          var textField: TextField = new TextField();
          //textField.htmlText = "<b>" + m_Progress.toFixed(0) + "%</b>";
          textField.text = m_Progress.toFixed(0)+"%";
          //Create a BitmapData object and take the Width and height of the TextField text.
          var textBitmapData: BitmapData = new BitmapData(textField.textWidth + 5, textField.textHeight);
          //Set the BitmapData object background color to the background color of the progress bar
          textBitmapData.floodFill(0, 0, progressBarBackground);
          //Draw the TextFiel object in the BitmapData object
          textBitmapData.draw(textField);
          //Construct the matrix object
          //The matrix object is used to place the text
          var textBitmapMatrix: Matrix = new Matrix();
          textBitmapMatrix.translate((px + (containerWidth) / 2)-(textField.textWidth/2), py + (progressHeight - textBitmapData.height) / 2);

          //Draw the sprite object on the parent BitmapData
          bitmapData.draw(sprite);

          //Draw the text on the parent BitmapData 
          bitmapData.draw(textBitmapData, textBitmapMatrix, null, null, null, false);

          return bitmapData;
        }

        public function set ready(value: Boolean): void
        {
          m_Ready = value;
          this.visible = !value;
        }

        public function get ready(): Boolean
        {
          return m_Ready;
        }

        public function set progress(value: Number): void
        {
          m_Progress = value;
        }

        public function get progress(): Number
        {
          return m_Progress;
        }
    }
}